﻿using System;

namespace CollegeSim.Domain.Entities
{
    // Author: Ryan Redburn
    // Project: CollegeSim
    // Date: 5/6/2014

    #region [ Updates ]

    // Author; Ryan Redburn
    // Date: 5/18/2014
    // Revisions: Added addtional logic to ID properties and validation to object properties.

    #endregion

    /// <summary>
    /// Class used to represent a course session. Contains properties, ToString() override.
    /// </summary>
    public class Session
    {
        #region [ Fields ]

        private string _crn;
        private int _capacity;
        private Course _course;
        private Instructor _instructor;
        private Semester _semester;
        private SchoolYear _schoolYear;

        #endregion

        #region [ Properties ]
        
        // SessionID
        // Set by DB
        public int SessionID { get; set; }

        /// <summary>
        /// Use to get CourseID.
        /// </summary>
        public int CourseID { get; private set; }

        /// <summary>
        /// Use to get InstructorID.
        /// </summary>
        public int InstructorID { get; private set; }

        /// <summary>
        /// Use to get SemesterID.
        /// </summary>
        public int SemesterID { get; private set; }

        /// <summary>
        /// Use to get SchoolYearID.
        /// </summary>
        public int SchoolYearID { get; private set; }

        /// <summary>
        /// Four character course reference number
        /// </summary>
        public string CRN
        {
            get { return _crn; }
            set
            {
                if (!String.IsNullOrWhiteSpace(value))
                {
                    if (value.Length == 4)
                    {
                        _crn = value;
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException("value", "CRN must be four characters in length.");
                    }
                }
                else
                {
                    throw new ArgumentNullException("value", "CRN cannot be null or empty.");
                }
            }
        }

        /// <summary>
        /// Session student capacity
        /// </summary>
        public int Capacity
        {
            get { return _capacity; }
            set
            {
                if (value > 0 && value <= 50)
                {
                    _capacity = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException("value", "Capacity cannot be greater than fifty or less than one.");
                }
            }
        }

        /// <summary>
        /// Corresponding Session course.
        /// </summary>
        public Course Course
        {
            get { return _course; }
            set
            {
                if (value != null)
                {
                    _course = value;
                    CourseID = _course.CourseID;
                }
                else
                {
                    throw new ArgumentNullException("value", "A null Course cannot be assigned to a Session.");
                }
            }
        }

        /// <summary>
        /// Corresponding Session instructor.
        /// </summary>
        public Instructor Instructor
        {
            get { return _instructor; }
            set
            {
                if (value != null)
                {
                    _instructor = value;
                    InstructorID = _instructor.InstructorID;
                }
                else
                {
                    throw new ArgumentNullException("value", "A null Instructor cannot be assigned to a Session.");
                }
            }
        }

        /// <summary>
        /// Corresponding Session semester.
        /// </summary>
        public Semester Semester
        {
            get { return _semester; }
            set
            {
                if (value != null)
                {
                    _semester = value;
                    SemesterID = _semester.SemesterID;
                }
                else
                {
                    throw new ArgumentNullException("value", "A null Semester cannot be assigned to a Session.");
                }
            }
        }

        /// <summary>
        /// Correspdoning Session school year.
        /// </summary>
        public SchoolYear SchoolYear
        {
            get { return _schoolYear; }
            set
            {
                if (value != null)
                {
                    _schoolYear = value;
                    SchoolYearID = _schoolYear.SchoolYearID;
                }
                else
                {
                    throw new ArgumentNullException("value", "A null SchoolYear cannot be assigned to a Session.");
                }
            }
        }
        

        #endregion

        #region [ Constructors ]

        // Initialize identifiers
        public Session()
        {
            SessionID = 0;
            CourseID = 0;
            InstructorID = 0;
            SemesterID = 0;
            SchoolYearID = 0;
        }

        #endregion

        #region [ Methods ]

        /// <summary>
        /// Returns a string representation of a session. CRN - Sub/Num - Title - Semester SchoolYear
        /// </summary>
        /// <returns>CRN - Sub/Num - Title - Semester SchoolYear</returns>
        public override string ToString()
        {
            return CRN + " - " + Course.Subject + Course.Number + " - " + Course.Title + " - " + Semester.Name + " " +
                   SchoolYear.Start.Year + "-" + SchoolYear.End.Year;
        }

        #endregion
    }
}
